package com.example.nomemo;

import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

public class NoticeManager {
    private DBHelper dbHelper;
    private String TBNAME;

    public NoticeManager(Context context) {
        dbHelper = new DBHelper(context);
        TBNAME = DBHelper.TB_NAME;
    }
    public void add(NoticeItem item){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("notTitle", item.getNotTitle());
        values.put("notTime", item.getNotTime());
        db.insert(TBNAME, null, values);
        db.close();
    }

    public void deleteAll(){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        db.delete(TBNAME,null,null);
        db.close();
    }

    public void delete(int id){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        db.delete(TBNAME, "ID=?", new String[]{String.valueOf(id)});
        db.close();
    }

    public void addAll(List<NoticeItem> list){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        for (NoticeItem item : list) {
            ContentValues values = new ContentValues();
            values.put("notTitle", item.getNotTitle());
            values.put("notTime", item.getNotTime());
            db.insert(TBNAME, null, values);
        }
        db.close();
    }

    public List<NoticeItem> listAll(){
        List<NoticeItem> noticeList = null;
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(TBNAME, null, null, null, null, null, null);
        if(cursor!=null){
            noticeList = new ArrayList<NoticeItem>();
            while(cursor.moveToNext()){
                NoticeItem item = new NoticeItem();
                item.setId(cursor.getInt(cursor.getColumnIndex("ID")));
                item.setNotTitle(cursor.getString(cursor.getColumnIndex("notTitle")));
                item.setNotTime(cursor.getString(cursor.getColumnIndex("notTime")));

                noticeList.add(item);
            }
            cursor.close();
        }
        db.close();
        return noticeList;

    }
}
